package com.bw.car.page

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.car.R
import com.bw.car.databinding.ActivityPaymentBinding
import com.bw.car.entity.PayNewEntity
import com.bw.car.vm.CarPayViewModel
import com.bw.common.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.util.Calendar
import java.util.Locale

@Route(path = RouterPath.PATH_PAYMENT_PATH)
@AndroidEntryPoint
class PaymentActivity : BaseMvvmActivity<ActivityPaymentBinding>() {

    @Autowired
    @JvmField
    var carCode:String = ""
    @Autowired
    @JvmField
    var carTime:String = ""


    override fun getLayoutId(): Int = R.layout.activity_payment
    private val mViewModel:CarPayViewModel by viewModels()

    override fun initViews() {
        super.initViews()
        ARouter.getInstance().inject( this)

        mDataBinding.carCode.setText(carCode)
        mDataBinding.myCarCode.setText(carCode)
        mDataBinding.myCarTime.setText(carTime)
        mDataBinding.carTime1.setText(carTime)

        mDataBinding.butPayment.setOnClickListener {
            // 获取当前时间的毫秒数
            val currentTimeMillis = System.currentTimeMillis()
            // 定义时间格式
            val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault())
            // 格式化当前时间
            val currentTimeStr = sdf.format(currentTimeMillis)
            println("当前时间：$currentTimeStr")
            // 使用 Calendar 给小时加 1
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = currentTimeMillis // 设置当前时间
            calendar.add(Calendar.HOUR_OF_DAY, -1) // HOUR_OF_DAY 表示 24 小时制
            // 格式化加 1 小时后的时间
            val newTimeStr = sdf.format(calendar.time)
            println("加 1 小时后：$newTimeStr")

            val thisTime = SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(System.currentTimeMillis())
            val payNew = PayNewEntity(
                entryTime = newTimeStr,
                exitTime = thisTime,
                id =0,
                parkingDuration =60,
                parkingSpaceNumber ="A10",
                paymentAmount =45,
                paymentMethod = 1,
                paymentTime =thisTime,
                paymentType =1,
                plateNumber =carCode,
                reservationId = 1
            )
            mViewModel.getPaymentData(payNew)

        }


    }

    override fun loadData() {
        super.loadData()
        mViewModel.mPayNewSuccess.observe(this){
            ARouter.getInstance().build(RouterPath.PATH_STOP_PATH).navigation()
            ToastUtils.showLong("支付成功")
        }
    }
}